<template>
	<view>
		<scroll-view scroll-y class="page">
			<!-- 头部 -->
			<cu-custom bgColor="bg-rgb" :isBack="true">
				<block slot="backText">返回</block>
				<block slot="content">发货详情</block>
				<view slot="right" @tap="rightClick">凭证抬头</view>
			</cu-custom>
			<!-- 查询条件 -->
			<view class="fixed" :style="[{animationDelay: 0.1 + 's'}]">
				<form>
					<view class="cu-form-group last">
						<image class="icon" src="../../static/saomiao.png" mode=""></image>
						<input auto-focus placeholder="请扫描料号" name="input" v-model="myFormData.oddNumber" @confirm="inputQuery"></input>
					</view>
				</form>
			</view>
			<!-- 详情 -->
			<view class="list">
				<!-- 主数据 -->
				<!-- <view class="cu-list menu" v-if="JSON.stringify(this.releaseMsg) !=='{}'">
					<view class="cu-item animation-slide-bottom" :style="[{animationDelay: '0.1s'}]">
						<view class="content">
							<text class="title" style="background-color: #0081FF;">发货单</text>
						</view>
					</view>
					<view class="cu-item animation-slide-bottom" :style="[{animationDelay: '0.2s'}]">
						<view class="content">
							<text class="text-grey">订单编号</text>
						</view>
						<view class="action">
							<text class="text-grey">{{releaseMsg.orderCode}}</text>
						</view>
					</view>
					<view class="cu-item animation-slide-bottom" :style="[{animationDelay: '0.3s'}]">
						<view class="content">
							<text class="text-grey">订单类型</text>
						</view>
						<view class="action">
							<text class="text-grey">{{releaseMsg.orderType}}</text>
						</view>
					</view>
					<view class="cu-item animation-slide-bottom" :style="[{animationDelay: '0.4s'}]">
						<view class="content">
							<text class="text-grey">订单名称</text>
						</view>
						<view class="action">
							<text class="text-grey">{{releaseMsg.orderName}}</text>
						</view>
					</view>
					<view class="cu-item animation-slide-bottom" :style="[{animationDelay: '0.5s'}]">
						<view class="content">
							<text class="text-grey">请求交货日期</text>
						</view>
						<view class="action">
							<text class="text-grey">{{releaseMsg.deliveryDate}}</text>
						</view>
					</view>
					<view class="cu-item animation-slide-bottom" :style="[{animationDelay: '0.6s'}]">
						<view class="content">
							<text class="text-grey">售达方编号</text>
						</view>
						<view class="action">
							<text class="text-grey">{{releaseMsg.saleCode}}</text>
						</view>
					</view>
					<view class="cu-item animation-slide-bottom" :style="[{animationDelay: '0.7s'}]">
						<view class="content">
							<text class="text-grey">售达方</text>
						</view>
						<view class="action">
							<text class="text-grey">{{releaseMsg.saleName}}</text>
						</view>
					</view>
				</view> -->
				<!-- 子订单 -->
				<view class="cu-list menu list" v-if="subOrder.length > 0">
					<view class="cu-list menu animation-slide-bottom" v-for="(item,index) in subOrder" :style="[{animationDelay: (index + 1)*0.1 + 's'}]"
					 :key="index">
						<!-- <view class="cu-item">
							<view class="title">
								<text>行号({{item.rowNum}})</text>
							</view>
						</view> -->
						<view class="cu-item">
							<view class="content">
								<text class="text-grey">料号</text>
							</view>
							<view class="action">
								<text class="text-grey">{{item.materielCode}}</text>
							</view>
						</view>
						<view class="cu-item">
							<view class="content">
								<text class="text-grey">物料</text>
							</view>
							<view class="action">
								<text class="text-grey">{{item.materielName}}</text>
							</view>
						</view>
						<view class="cu-item">
							<view class="content">
								<text class="text-grey">类型</text>
							</view>
							<view class="action">
								<text class="text-grey">{{item.type}}</text>
							</view>
						</view>
						<view class="cu-item">
							<view class="content">
								<text class="text-grey">工厂名称</text>
							</view>
							<view class="action">
								<text class="text-grey">{{item.factoryName}}</text>
							</view>
						</view>
						<view class="cu-item">
							<view class="content">
								<text class="text-grey">客户物料编号</text>
							</view>
							<view class="action">
								<text class="text-grey">{{item.clientMaterielcode}}</text>
							</view>
						</view>
						<view class="cu-item">
							<view class="content">
								<text class="text-grey">订单数量</text>
							</view>
							<view class="action">
								<text class="text-grey">{{item.orderNum}}</text>
							</view>
						</view>
						<view class="cu-item">
							<view class="content">
								<text class="text-grey">未发货数量</text>
							</view>
							<view class="action">
								<text class="text-grey">{{item.undeliveryNum}}</text>
							</view>
						</view>
						<view class="cu-item">
							<view class="content">
								<text class="text-grey">单位</text>
							</view>
							<view class="action">
								<text class="text-grey">{{item.unit}}</text>
							</view>
						</view>
						<view class="cu-item">
							<view class="content">
								<text class="titles" style="background-color: #99CC00;">发货数量：</text>
							</view>
							<view class="action">
								<input type="text" v-model="preservationList[index].num" value="" placeholder="请输入发货数量" />
							</view>
						</view>
						<view class="cu-item">
							<view class="content">
								<text class="text-grey">箱数</text>
							</view>
							<view class="action">
								<input type="text" v-model="preservationList[index].query5" value="" placeholder="请输入箱数箱数" />
							</view>
						</view>
						<view class="cu-item">
							<view class="content">
								<text class="text-grey">毛重</text>
							</view>
							<view class="action">
								<input type="text" v-model="preservationList[index].query6" value="" placeholder="请输入毛重" />
							</view>
						</view>
						<view class="cu-item btns">
							<!-- <view class="btn1" @click="returnGoods(index,item)">退货</view> -->
							<view class="btn2" @click="deliverGoods(index,item)">发货</view>
							<!-- <view class="btn3" @click="scanShipment(index,item)">扫描发货</view> -->
						</view>
					</view>
				</view>
			</view>
			<!-- 抬头凭证 输入弹窗 -->
			<view class="cu-modal" :class="modalName=='Modal'?'show':''">
				<view class="cu-dialog">
					<view class="cu-bar bg-white justify-end">
						<view class="content">凭证抬头</view>
						<view class="action">
							<text class="cuIcon-close text-red" @click="close"></text>
						</view>
					</view>
					<view class="padding-xl">
						<form>
							<view class="cu-form-group">
								<view class="titles">凭证日期：</view>
								<picker mode="date" :value="myModelData.cerDate" @change="cerDateChange">
									<view class="picker">
										{{myModelData.cerDate}}
									</view>
								</picker>
							</view>
							<view class="cu-form-group">
								<view class="titles">过账日期：</view>
								<picker mode="date" :value="myModelData.accountDate" @change="accountDateChange">
									<view class="picker">
										{{myModelData.accountDate}}
									</view>
								</picker>
							</view>
							<view class="cu-form-group">
								<view class="titles">类型：</view>
								<input placeholder="工号" style="text-align: right;" name="input" v-model="myModelData.affairType" disabled></input>
							</view>
							<view class="cu-form-group">
								<view class="titles">描述：</view>
								<textarea value="" placeholder="请输入描述" style="text-align: left;text-indent: 2em;" v-model="myModelData.affairContent" />
								</view>
							<view class="padding flex flex-direction">
								<button class="cu-btn lg"  @click="onSubmit">确定</button>
							</view>
						</form>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		name: 'releaseDetail',
		data() {
			return {
				id: '',
				// 输入框查询
				myFormData: {
					oddNumber: ''
				},
				// 主数据
				// releaseMsg:{},
				// 子订单数据
				subOrder:[],
				// 有一个子订单  次数数组就有几个对象 用于存储id + 发货数量
				preservationList:[],
				// 控制凭证抬头显示 隐藏
				modalName:'',
				// 凭证抬头 绑定值
				myModelData:{
					cerDate:'', //凭证日期
					accountDate:'', // 过账日期
					affairType:'',
					affairContent:''
				},
			}
		},
		onShow() {
			this.getSubOrder();
		},
		onLoad(e) {
			this.id = uni.getStorageSync('releaseId'); // 获取缓存值
			// this.getReleaseDetail();
			// this.getSubOrder();
			let date = new Date()
			this.myModelData.cerDate = date.getFullYear() + '-' +(date.getMonth() + 1) + '-' + date.getDate();
			this.myModelData.accountDate = date.getFullYear() + '-' +(date.getMonth() + 1) + '-' + date.getDate();
			this.myModelData.affairType = uni.getStorageSync('affairType');
		},
		methods: {
			// 订单管理—采购订单-通过id查询
			// getReleaseDetail() {
			// 	let data = {
			// 		id: this.id
			// 	}
			// 	this.$http.get('order/mesOrderSale/queryById',{params:data}).then(res => {
			// 		console.log(res)
			// 		if (res.data.success) {
			// 			this.releaseMsg = res.data.result;
			// 		} else {
			// 			this.$tip.error(res.data.message)
			// 		}
			// 	})
			// },
			// 订单管理—销售订单子表-通过主表ID查询
			getSubOrder(){
				uni.showLoading({
					mask:true,
					title:'加载中.....'
				})
				let data = {
					saleorderId: this.id,
					state:'未完成'
				}
				this.$http.get('order/mesOrderSale/listMesSaleItemByMainId',{params:data}).then(res => {
					console.log(res)
					if (res.data.success) {
						uni.hideLoading()
						this.subOrder = res.data.result.records;
						this.preservationList = []
						for (let i = 0; i < this.subOrder.length; i++) {
							let data = {
								id: this.subOrder[i].id,
								num: ''
							}
							this.preservationList.push(data)
						}
						console.log(this.subOrder)
					} else {
						// this.$tip.error(res.data.message)
            this.modal.show({ type:'error', content:res.data.message })
					}
				})
			},
			// 发货
			deliverGoods(index,item){
				if (this.preservationList[index].num == '' || this.preservationList[index].num == 0) {
					// this.$tip.error('发货数量不能为空')
          this.modal.show({ type:'error', content:'发货数量不能为空' })
					return false
				}else if(!uni.getStorageSync('releaseDetailRise')){
					this.modalName = 'Modal'
					return false
				}
				uni.showModal({
					title: '发货',
					content: '是否发货？',
					cancelText: '取消',
					confirmText: '发货',
					success: res => {
						if (res.confirm) {
							uni.showLoading({
								title:'发货中....',
								mask:true
							})
							let data = {
								mobileCode:601,
								materielCode:item.materielCode,
								mobileType:'发货',
								perkId:uni.getStorageSync('releaseDetailRise'),
								reserveCode:this.preservationList[index].id,
								inputNum:this.preservationList[index].num,
								query5:this.preservationList[index].query5,
								query6:this.preservationList[index].query6,
							}
							// this.$http.post('storage/mesCertificatePerk/addMesCertificateItem',data).then( res =>{
							this.$http.post('storage/mesCertificatePerk/addSendProduct',data).then( res =>{
								uni.hideLoading()
								console.log(res)
								if(res.data.success){
									// this.$tip.success(res.data.message);
									this.modal.show({ type:'success', content:res.data.message })
									this.getSubOrder();
								}else {
									// this.$tip.error(res.data.message)
                  this.modal.show({ type:'error', content:res.data.message })
								}
							})
						}
					}
				})
			},
			// 扫描发货
			scanShipment(index,item){
				if(!uni.getStorageSync('releaseDetailRise')){
					this.modalName = 'Modal'
					return false
				}
				uni.showModal({
					title: '发货',
					content: '是否扫描发货？',
					cancelText: '取消',
					confirmText: '扫描发货',
					success: res => {
						if (res.confirm) {
							uni.showLoading({
								title:'发货中....',
								mask:true
							})
							let data = {
								mobileCode:603,
								materielCode:item.materielCode,
								mobileType:'扫描发货',
								perkId:uni.getStorageSync('releaseDetailRise'),
								reserveCode:this.preservationList[index].id,
								inputNum:this.preservationList[index].num
							}
							// this.$http.post('storage/mesCertificatePerk/addMesCertificateItem',data).then( res =>{
							this.$http.post('storage/mesCertificatePerk/scanSendProduct',data).then( res =>{
								uni.hideLoading()
								if(res.data.success){
									uni.setStorageSync('releaseDetailId',res.data.result.id)
									uni.setStorageSync('releaseDetailItemId',item.id)
									this.$Router.push({
										name: 'releaseDetailSaoMiao'
									})
								}else {
									// this.$tip.error(res.data.message)
                  this.modal.show({ type:'error', content:res.data.message })
								}
							})
						}
					}
				})
			},
			// 退货
			returnGoods(index,item){
				if (this.preservationList[index].num == '' || this.preservationList[index].num == 0) {
					// this.$tip.error('退货数量不能为空')
          this.modal.show({ type:'error', content:'退货数量不能为空' })
					return false
				}else if(!uni.getStorageSync('releaseDetailRise')){
					this.modalName = 'Modal'
					return false
				}
				uni.showModal({
					title: '发货',
					content: '是否退货？',
					cancelText: '取消',
					confirmText: '退货',
					success: res => {
						if (res.confirm) {
							uni.showLoading({
								title:'退货中....',
								mask:true
							})
							let data = {
								mobileCode:602,
								materielCode:item.materielCode,
								mobileType:'退货',
								perkId:uni.getStorageSync('releaseDetailRise'),
								reserveCode:this.preservationList[index].id,
								inputNum:this.preservationList[index].num
							}
							// this.$http.post('storage/mesCertificatePerk/addMesCertificateItem',data).then( res =>{
							this.$http.post('storage/mesCertificatePerk/addReturnProduct',data).then( res =>{
								uni.hideLoading()
								if(res.data.success){
									// this.$tip.success(res.data.message);
									this.modal.show({ type:'success', content:res.data.message })
									this.getSubOrder();
								}else {
									// this.$tip.error(res.data.message)
                  this.modal.show({ type:'error', content:res.data.message })
								}
							})
						}
					}
				})
			},
			// 凭证抬头 按钮
			rightClick(){
				this.modalName = 'Modal'
			},
			// 关闭凭证抬头
			close(){
				this.modalName = '';
				this.myModelData.affairContent = '';
			},
			// 凭证日期
			cerDateChange(e) {
				console.log(e)
				this.myModelData.cerDate = e.detail.value
			},
			accountDateChange(e){
				this.myModelData.accountDate = e.detail.value
			},
			// 保存 凭证抬头  缓存凭证抬头ID
			onSubmit(){
				uni.showLoading({
					mask:true,
					title:'添加凭证抬头中.....'
				})
				if(this.myModelData.affairContent.replace(/\s+/g, "") == ''){
					// this.$tip.error('描述不能为空');
					this.modal.show({ type:'error', content:'描述不能为空' })
					uni.hideLoading()
					return false
				}
				this.$http.post('storage/mesCertificatePerk/add',this.myModelData).then( res =>{
					uni.hideLoading()
					if(res.data.success){
						uni.setStorageSync('releaseDetailRise',res.data.result.id);
						// this.$tip.success(res.data.message)
            this.modal.show({ type:'success', content:res.data.message })
						this.modalName = '';
						this.myModelData.affairContent = '';
					}else {
						// this.$tip.error(res.data.message)
            this.modal.show({ type:'error', content:res.data.message })
					}
				})
			},
		},
	}
</script>

<style lang="less">
	page {
		background-color: white;
		.fixed {
			position: fixed;
			z-index: 1;
			width: 100%;
			background-color: white;
			height: 100rpx;
			border-bottom: 1rpx solid #eee;

			.last {
				border-bottom: 1rpx solid #eee;
				position: relative;

				.icon {
					width: 30rpx;
					height: 29rpx;
					position: absolute;
					top: 36rpx;
					left: 60rpx;
				}

				input {
					width: 686rpx;
					height: 68rpx;
					background: #FFFFFF;
					border: 1px solid #D5D8DF;
					border-radius: 34px;
					padding-left: 79rpx;
				}
			}
		}
		.list {
			margin-top: 100rpx;
			.title {
				font-weight: bold;
				padding: 10rpx;
				font-size: 28rpx;
			}
			.titles{
				font-weight: bold;
				padding: 10rpx;
				font-size: 28rpx;
				border-radius: 10rpx;
				color: white;
			}
			.content{
				color: #485A75;
			}
			.action{
				width: 70%;
				text-align: right;
				word-break: break-all;
				.text-grey{
				color: #191919;
				font-weight: bold;
				}
				input{
					width: 300rpx;
					border: 2rpx solid #eee;
					padding:0 30rpx;
					height: 60rpx;
					border-radius: 10rpx;
				}
			}
			.line {
				width: 750rpx;
				height: 10rpx;
				background: #F3F5F9;
			}
			.btns {
				padding: 0 20rpx;
				font-size: 25rpx;
				padding: 20rpx 30rpx;

				view {
					width: 180rpx;
					height: 64rpx;
					line-height: 50rpx;
					color: white;
					border-radius: 30rpx;
					padding: 10rpx 20rpx;
					text-align: center;
				}

				.btn1 {
					background-image: radial-gradient(circle, #3F55E9, #5F71ED, #7D8CF0);
				}

				.btn2 {
					background-image: radial-gradient(circle, #716BA9, #362D85, #57509A);
				}

				.btn3 {
					background-image: radial-gradient(circle, #354D9E, #354D9E, #697BB7);
				}
			}
		}
	}
	.cu-btn{
		background-image: radial-gradient(circle, #3F55E9, #5F71ED, #7D8CF0);
		color: white;
	}
</style>
